package com.qige.dome.homework;

/**
 * @Author qige
 * @Date 2024-08-07 20:20
 * @Description
 */
public class homework7 {
    /*
    2.编写多线程程序，模拟多个人通过一个山洞的模拟。
    这个山洞每次只能通过一个人，每个人通过山洞的时间为5秒，有10个人同时准备过此山洞，显示每次通过山洞人的姓名和顺序。
     */
    public static void main(String[] args) {
        MyThread3 myThread3 = new MyThread3();
        Thread t1 = new Thread(myThread3, "张三");
        Thread t2 = new Thread(myThread3, "李四");
        Thread t3 = new Thread(myThread3, "王五");
        Thread t4 = new Thread(myThread3, "赵六");
        Thread t5 = new Thread(myThread3, "钱七");
        Thread t6 = new Thread(myThread3, "孙八");
        Thread t7 = new Thread(myThread3, "周九");
        Thread t8 = new Thread(myThread3, "吴十");
        Thread t9 = new Thread(myThread3, "郑十一");
        Thread t10 = new Thread(myThread3, "冯十二");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();
        t8.start();
        t9.start();
        t10.start();
    }
}
class MyThread3 implements Runnable {
    private int count;
    @Override
    public void run() {
        cross();
    }
    public synchronized void cross(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        count++;
        System.out.println(Thread.currentThread().getName() + "通过山洞了，这是第" + count + "次通过");
    }
}
